import {
  getArticleList,
  postArticle,
  putArticle,
  deleteArticle
} from '@/api/article'
import {
  getToken,
  setToken,
  removeToken
} from '@/utils/auth'
import {
  resetRouter
} from '@/router'

const getDefaultState = () => {
  return {}
}
const state = getDefaultState()
const mutations = {
  RESET_STATE: (state) => {
    Object.assign(state, getDefaultState())
  }
}

const actions = {
  // user login
  getArticleList({
    commit
  }, data) {
    return new Promise((resolve, reject) => {
      getArticleList(data).then(response => {
        resolve(response)
      }).catch(error => {
        console.log(error)
        reject(error)
      })
    })
  },
  postArticle({
    commit
  }, data) {
    return new Promise((resolve, reject) => {
      postArticle(data).then(response => {
        resolve(response)
      }).catch(error => {
        console.log(error)
        reject(error)
      })
    })
  },
  putArticle({
    commit
  }, data) {
    return new Promise((resolve, reject) => {
      putArticle(data).then(response => {
        resolve(response)
      }).catch(error => {
        console.log(error)
        reject(error)
      })
    })
  },
  deleteArticle({
    commit
  }, data) {
    return new Promise((resolve, reject) => {
      deleteArticle(data).then(response => {
        resolve(response)
      }).catch(error => {
        console.log(error)
        reject(error)
      })
    })
  }
}
export default {
  namespaced: true,
  state,
  mutations,
  actions
}
